<?php

/**
 * This is the model class for table "stat_online".
 *
 * The followings are the available columns in table 'stat_online':
 * @property string $id
 * @property string $session_id
 * @property integer $session_start
 * @property integer $last_active
 * @property string $url
 * @property string $url_referrer
 * @property string $agent
 * @property string $ip
 * @property integer $user_id
 */
class StatOnline extends MyAR
{

    public $log_activity = FALSE;

    const TIMEOUT = 300;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return StatOnline the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{stat_online}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('session_start, last_active, user_id', 'numerical', 'integerOnly' => true),
            array('session_id', 'length', 'max' => 50),
            array('ip', 'length', 'max' => 30),
            array('url, url_referrer, agent', 'length', 'min' => 3),
//            array('url, url_referrer, agent', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, session_id, session_start, last_active, url, url_referrer, agent, ip, user_id', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        $array = array(
            'id'            => 'ID',
            'session_id'    => Yii::t('main', 'Session'),
            'session_start' => Yii::t('main', 'Session Start'),
            'last_active'   => Yii::t('main', 'Last Active'),
            'url'           => Yii::t('main', 'Url'),
            'url_referrer'  => Yii::t('main', 'Url Referrer'),
            'agent'         => Yii::t('main', 'Agent'),
            'ip'            => Yii::t('main', 'Ip'),
            'user_id'       => Yii::t('main', 'User'),
        );

        foreach ($array as $key => $val)
        {
            $array[$key] = Yii::t('supportonline', $val);
        }

        return $array;
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('session_id', $this->session_id, true);
        $criteria->compare('session_start', $this->session_start);
        $criteria->compare('last_active', $this->session_start);
        $criteria->compare('url', $this->url, true);
        $criteria->compare('url_referrer', $this->url_referrer, true);
        $criteria->compare('agent', $this->agent, true);
        $criteria->compare('ip', $this->ip, true);
        $criteria->compare('user_id', $this->user_id);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public static function runRemote($url, $url_refer)
    {
        // Đếm số người online
        if (session_id() == '')
        {
            session_start();
        }
        $session = session_id();

        $online = StatOnline::model()->find("session_id=:session_id", array(
            'session_id' => $session
        ));

        if ($online == NULL)
        {
            $online                = new StatOnline;
            $online->session_id    = $session;
            $online->session_start = time();
        }

        $online->last_active  = time();
        $online->url          = $url;
        $online->url_referrer = $url_refer;
        $online->agent        = Yii::app()->request->userAgent;
        $online->ip           = ip2long(Yii::app()->request->userHostAddress);
        $online->save();
    }

    public static function run()
    {
        $url       = Yii::app()->request->requestUri;
        $url_refer = Yii::app()->request->urlReferrer;

        self::runRemote($url, $url_refer);
    }

    public static function countUserOnline($range = self::TIMEOUT)
    {
        $limit = time() - $range;
        return self::model()->count("last_active > $limit");
    }

    public static function countVisitor($limit)
    {
        return self::model()->count("session_start > $limit");
    }

    public static function getUserOnline($range = self::TIMEOUT)
    {
        $limit = time() - $range;
        return self::model()->findAll("last_active > $limit");
    }

}
